#include <iostream>
using namespace std;

const int N = 1e5 + 5;

int arr[N], n, m;

void adjust_down(int p)
{
    int c = p * 2 + 1;
    while(c < n)
    {
        if(c + 1 < n && arr[c + 1] < arr[c]) c++;
        if(arr[c] < arr[p]) swap(arr[p], arr[c]);
        p = c;
        c = p * 2 + 1;
    }
}

int main()
{
    cin >> n >> m;
    for(int i = 0; i < n; ++i) cin >> arr[i];
    
    for(int i = (n - 2) / 2; i >= 0; --i) adjust_down(i);
    
    for(int i = 0; i < m; ++i)
    {
        cout << arr[0] << ' ';
        swap(arr[0], arr[--n]);
        adjust_down(0);
    }
    
    return 0;
}